//
// Created by 12992 on 24-10-13.
//

#ifndef FRAME_COUNTER_H
#define FRAME_COUNTER_H

#include "video_meta_error.hpp"
#include "media_frame.h"
#include "media_packet.h"
#include <string>
#include <vector>

extern "C"
{
struct AVFormatContext;
struct AVStream;
struct AVPacket;
struct AVCodecContext;
}

namespace utils
{
    class video_meta
    {
    public:
        explicit video_meta(const std::string& input);

        ~video_meta();

        int get_total_frame();

        int get_stream_frame(int index);

        std::string get_video_format() const;

        std::string get_video_format2() const;

        std::string get_video_encoder_type() const;

        std::string get_video_color_space() const;

        std::vector<utils::media_frame> get_video_preview_frame() const;

        int get_video_framerate() const;

        long long int get_video_avg_bitrate() const;

        long long int get_duration() const;

        std::pair<int, int> get_video_resolution() const;

        std::string get_audio_encoder_type() const;

        std::string get_audio_channel_layout() const;

        int get_audio_sample_rate() const;

        int get_audio_stream_number() const;

        std::string get_filename() const;

        std::string get_path()const;

    private:
        void clean_frame_counter(error::video_meta_error::type _type);

        void count_frame();

        media_packet read_one_frame(int index) const;

        std::vector<utils::media_frame>
        flush_all_frame(AVCodecContext* codec_ctx, std::vector<media_packet>& packets) const;

        std::vector<utils::media_frame> change_color_space(std::vector<utils::media_frame>& frames) const;

    private:
        std::string input_path;
        std::string video_name;
        AVFormatContext* m_fmt_ctx{};
        AVPacket* m_pkt{};
        std::vector<long long> m_frames;
        long long total_frame{-1};
    };
} // utils

#endif //FRAME_COUNTER_H
